package com.jhws.sys.appUpgrade.controller;

import com.jhws.common.base.bo.JsonRespData;
import com.jhws.sys.appUpgrade.bo.TblSoftLabelDimBo;
import com.jhws.sys.appUpgrade.service.ITblSoftLabelDimService;
import com.jhws.util.GetId;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/softLabel")
public class SoftLabelCtrl {
    @Resource
    protected ITblSoftLabelDimService ISoftLabelService;

    @RequestMapping("query.x")
    public String query()
            throws Exception {
        return "business/soft/queryLabel";
    }

    @RequestMapping("queryList.x")
    @ResponseBody
    public JsonRespData queryList(@RequestParam Map param) {
        Map data = ISoftLabelService.queryResourcePage(param);
        return new JsonRespData("", 1, data);
    }

    @RequestMapping("querySoftLabels.x")
    @ResponseBody
    public JsonRespData querySoftLabels() throws Exception {
        List<TblSoftLabelDimBo> softLabelDimBos = ISoftLabelService.findTblSoftLabelDimByWhere("where 1=1 ");
        return new JsonRespData("", 1, softLabelDimBos);
    }

    @RequestMapping("remove.x")
    @ResponseBody
    public JsonRespData remove(HttpServletRequest request) throws Exception {
        String softLabelCode = request.getParameter("softLabelCode");
        this.ISoftLabelService.removeDim(softLabelCode);
        return new JsonRespData("", 1, "删除成功");
    }


    @RequestMapping("save.x")
    @ResponseBody
    public JsonRespData save(TblSoftLabelDimBo bo) throws Exception {
        // 从数据库查询标签名是否已经使用
        List<TblSoftLabelDimBo> sList = ISoftLabelService
                .findTblSoftLabelDimByWhere("where soft_labe_name = '" + bo.getSoftLabeName() + "'");
        if (null != sList && sList.size() > 0) {
            return new JsonRespData("", -1, "标签名已经存在，请重新输入!");
        }

        // 从数据库查询标签是否已经使用
        sList = ISoftLabelService
                .findTblSoftLabelDimByWhere("where soft_labe = '" + bo.getSoftLabe() + "'");
        if (null != sList && sList.size() > 0) {
            return new JsonRespData("", -1, "标签名已经存在，请重新输入!");
        }

        bo.setSoftLabelCode(GetId.randomID());
        ISoftLabelService.save(bo);
        return new JsonRespData("", 1, "添加标签信息成功");
    }
}
